<? if ($c = stack('name')) {

	if (($uuid = gccHuuid(param('edit'))) && 
		($d = datafetch('db_item', 'cs_claim', "id=$uuid AND person_id=".gccUserInfo('person_id')))) {
	
		require_once('_lib/formvalidator.php');

	
		$fields = array(
			'region_id' => 'req:Потрібно обрати',
			'issue' => 'req:Потрібно заповнити',
		);
	
		$_values	= array('question_id', 'region_id', 'issue', 'target');

		$_record = array('person_id' => gccUserInfo('person_id')); 
		foreach($_values as $v) $_record[$v] = trim(postdata($v));
		

		switch (postdata('edit_type')) {
			
			case 'save':
			
				$val = new FormValidator();
				
				foreach($fields as $f => $v) 
					foreach(explode(',', $v) as $rule) {
						$r = explode(':', $rule);
						$val->addValidation($f, $r[0], $r[1]);
					}
			
			//dbdebug(1,1);
				
				if ($val->ValidateForm()) {
					
			
					datafetch('db_update', 'cs_claim', $_record, "id=$uuid");
				
				
					$res = array();
					foreach($_values as $v)
						$res[] = array($f, true, $e);
					
					print(json_encode($res));
					exit;
				
				} else {
					
					$r = array();
				
					$res = $val->GetErrors();
					foreach($res as $f => $e)
						if (isset($r[$f]))
							$r[$f][2] .= " $e";
						else
							$r[$f] = array($f, false, $e);
					
					$res = array();
					foreach($r as $v)
						$res[] = $v;
						
					print(json_encode($res));
					exit;
				}
			
				break;
				
			case 'send':
	
	//debug($_record);
				$val = gccClaimStore($_record);
	//debug($val);			
				if ($val['stored']) {
					
					dataset('db_delete', 'cs_claim', "id=$uuid");

					$res = array();
					foreach($_values as $v)
						$res[] = array($v, true, '');
					
					print(json_encode($res));
					exit;
				
				} else {
				
					print($val['raw']);
					exit;
				}
				break;
		}

		
	} else {
	
		header("Location: "._transformURL($CURRENT->url, array('remove_params' => 'all')));
		exit;
	}

unstack();
}